let app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        images: [],
        title: '',
        show_time: '请选择日期',
        content: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var date = this.formatTime(new Date());
        // 再通过setData更改Page()里面的data，动态更新页面的数据
        this.setData({
            show_time: date
        });
    

        this.setData({ projectId: options.id });
    },


     formatTime:function(date) {
        var year = date.getFullYear()
        var month = date.getMonth() + 1
        var day = date.getDate()
        var hour = date.getHours()
        var minute = date.getMinutes()
        var second = date.getSeconds()
        return year + "-" + month + "-" + day;
      },

    /** 输入标题 */
    inputTitle(e){
        this.setData({ title: e.detail.value });
    },

    /** 选择时间 */
    changeDate(e){
        this.setData({ show_time: e.detail.value });
    },

    /** 输入内容 */
    inputArea(e){
        this.setData({ content: e.detail.value });
    },

    onChoosePic(){
        let that = this;
        wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
            // tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFilePaths;
            wx.uploadFile({
            url: `${app.globalData.rootUrl}/api/upload/upload`,
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {'appid': app.appid},
            success: res => {
                let response = JSON.parse(res.data);
                if(res.statusCode == 200){
                if(response.code == 0){
                    let images = that.data.images;
                    images.push(response.thumb);
                    that.setData({ images });
                }else{
                    app.showFailToast(response.msg);
                }
                }else app.showFailToast('网络错误，请稍后再试');
            }
            });
        }
        });
    },

    /** 取消 */
    onCancel(){
        wx.navigateBack();
    },

    onSumit(){
        // if(this.data.title == ''){
        //     app.showFailToast('请填写标题');
        //     return false;
        // }
        if(this.data.show_time == '请选择日期'){
            app.showFailToast('请选择时间');
            return false;
        }
        if(this.data.content == ''){
            app.showFailToast('请填写内容');
            return false;
        }
        // if(this.data.images.length == 0){
        //     app.showFailToast('请上传图片');
        //     return false;
        // }
        let params = {
            id: this.data.projectId,
            title: this.data.title,
            show_time: this.data.show_time,
            content: this.data.content,
            images: this.data.images.join(';')
        };
        // console.log(params)
        app.request('post', '/api/project/figure', params).then(res => {
            app.showSuccessToast('提交成功');
            setTimeout(() => {
                wx.navigateBack();
            }, 1500);
        }).catch(err => console.log(err));
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    previewImg1: function (e) {
        let that = this;
        // var figureList = e.currentTarget.dataset.v;
        let figureList = that.data.images;
        wx.previewImage({
          current: e.currentTarget.dataset.src,   //当前图片地址
          urls: figureList,        //所有要预览的图片的地址集合 数组形式
        });
      },
})